<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Add the file data for downloading.
 * 
 * 添加下载的资料信息及文件.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Inherited from Addaction.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_addaction.php';
require_once INSTALLDIR . '/lib/slh_rights.php';
require_once INSTALLDIR . '/lib/slh_datafile.php';

require_once INSTALLDIR . '/widgets/slh_form_adddata.php';

require_once INSTALLDIR . '/classes/Data.php';
require_once INSTALLDIR . '/classes/File.php';

class AdddataAction extends AddAction
{
	function title()
	{
		return '添加资料文件!';
	}
	
	function prepare($args)
	{
		if ( !Rights::hasRight(slh_get_userrights(), RIGHT_DATA)) {
			return false;
		}

		return parent::prepare($args);
	}
	
	/**
	 * Shows the guidance.
	 * Overriding.
	 */
	function showGuidance()
	{
		// Html code here.
		$this->element('h3', null, '添加资料文件');
		
		$this->instruction('呃,说明下添加的具体是什么东东,怎么添加好了.');
		
		// If added successfully, then?
		if ($this->added) {
			$this->element('p', 'cls_p_success', '添加成功!');
			// Or show a nav.
		}
	}
	
	/**
	 * Shows the widgets like form, or etc.
	 * Overriding.
	 */
	function showForm()
	{
		$form = new AdddataForm($this, $this->args);
		$form->show();
	}
	
	/**
	 * Try to add the data into the database.
	 */
	function tryAdd()
	{
		// Workaround for PHP returning empty $_POST and $_FILES when POST
		// length > post_max_size in php.ini
		if (empty($_FILES) &&
			empty($_POST) &&
			($_SERVER['CONTENT_LENGTH'] > 0)) {
			$msg = 'The server was unable to handle that mush POST data due to its current configuration.';
			$this->showAction($msg);
			return;
		}
		
		// CSRF protection and Refreshing-post protection.
		$this->checkSessionToken();
		
		$title		= $this->getTrimmedArg('title');
		$message	= $this->getTrimmedArg('message');
		
		// Validate the file first.
		try {
			$file = DataFile::fromUpload('datafile');
		} catch (Exception $e) {
			$this->showAction($e->getMessage());
			return;
		}
		
		if (false == $file) {
			$this->showAction('选择的文件不符合要求, 请重新选择上传的文件.');
		}
		else if (empty($title)) {
			$this->showAction('请输入资料名称.');
		} else {
			// Adds into the database.
			$data_id = Data::add(array('title' => $title,
									   'message' => $message,
									   'file' => $file));
			
			if ( !$data_id) {
				$this->showAction('添加下载资料失败!');
				return;
			}
			
			// Success.
			// Unset the seesion token preventing post-again.
			$this->unsetSessionToken();
			
			// Unset the seted values.
			unset($this->args['title']);
			unset($this->args['message']);
			unset($this->args['file']);
			
			$this->added = true;
			$this->showAction();
		}
	}
	
}

?>